package com.zzl.ov_library.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzl.ov_library.dto.Result;
import com.zzl.ov_library.entity.Comment;
import com.zzl.ov_library.entity.Reservation;
import com.zzl.ov_library.entity.ReservationVO;
import com.zzl.ov_library.service.CommentService;
import com.zzl.ov_library.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private ReservationService reservationService;

    @Autowired
    private ReservationController reservationController;

    //添加评论
    @PostMapping("/comment/add")
    public Result addComment(@RequestParam Integer reserveId, @RequestParam String content) {
        //通过reserveId查询预约记录是否已存在
        Reservation reservation = reservationService.getOne(Wrappers.lambdaQuery(Reservation.class)
                .eq(Reservation::getReserveId, reserveId));
        if (reservation == null) {
            return Result.fail().setMsg("预约记录不存在");
        }
        //通过reserveId查询评论记录
        Comment comment = commentService.getOne(Wrappers.lambdaQuery(Comment.class)
                .eq(Comment::getReserveId, reserveId));
        if (comment != null) {
            return Result.fail().setMsg("该预约记录已评论");
        }
        comment = new Comment();
        comment.setUserId(reservation.getUserId());
        comment.setBranchId(reservation.getBranchId());
        comment.setReserveId(reserveId);
        comment.setContent(content);
        return commentService.save(comment) ? Result.ok().setMsg("评论成功") : Result.fail().setMsg("评论失败");
    }

    //修改评论
    @PostMapping("/comment/update")
    public Result updateComment(@RequestParam Integer reserveId, @RequestParam String content) {
        //通过reservId查询评论
        Comment comment = commentService.getOne(Wrappers.lambdaQuery(Comment.class)
                .eq(Comment::getReserveId, reserveId));
        if (comment == null) {
            return Result.fail().setMsg("评论不存在");
        }
        comment.setContent(content);
        return commentService.updateById(comment) ? Result.ok().setMsg("修改成功") : Result.fail().setMsg("修改失败");
    }

    //通过用户id查询评论列表
    @PostMapping("/comment/listByUserId")
    public Result listComment(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "20") Integer limit,
                              @RequestParam Integer userId){
        //通过用户id查询评论,按照日期降序
        List<Comment> commentList = commentService.list(Wrappers.lambdaQuery(Comment.class)
                .eq(Comment::getUserId, userId)
                .orderByDesc(Comment::getId));
        if (commentList.isEmpty())
            return Result.fail().setMsg("该用户没有评论");
        List<Integer> reserveIds = commentList.stream().map(Comment::getReserveId).toList();
        //通过reserveIds查询预约记录
        List<ReservationVO> list = reservationController.getResult(reservationService.list(Wrappers.lambdaQuery(Reservation.class)
                .in(Reservation::getReserveId, reserveIds)
                .orderByDesc(Reservation::getReserveTime)));
        return Result.ok().setData(list);
    }

    //展示所有评论
    @PostMapping("/comment/list")
    public Result replyComment(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "20") Integer limit) {
        //查询评论
        List<Comment> commentList = commentService.list(new Page<>(page, limit));
        List<Integer> reserveIds = commentList.stream().map(Comment::getReserveId).toList();
        //通过reserveIds查询预约记录
        List<ReservationVO> list = reservationController.getResult(reservationService.list(Wrappers.lambdaQuery(Reservation.class)
                .in(Reservation::getReserveId, reserveIds)
                .orderByDesc(Reservation::getReserveTime)));
        return Result.ok().setData(list);
    }

    //管理员回复评论
    @PostMapping("/comment/reply")
    public Result replyComment(@RequestParam Integer id, @RequestParam String replay, @RequestParam Integer replayAdminId) {
        System.out.println("id:" + id);
        System.out.println("replay:" + replay);
        System.out.println("replayAdminId:" + replayAdminId);
        //通过Id查询评论
        Comment comment = commentService.getOne(Wrappers.lambdaQuery(Comment.class)
                .eq(Comment::getId, id));
        if (comment == null) {
            return Result.fail().setMsg("评论不存在");
        }
        //获取当前时间
        comment.setReplayTime(String.valueOf(new java.sql.Date(System.currentTimeMillis())));
        comment.setReplay(replay);
        comment.setReplayAdminId(replayAdminId);
        return commentService.updateById(comment) ? Result.ok().setMsg("回复成功") : Result.fail().setMsg("回复失败");
    }

}

